package com.gph.saviorframework.mq.controller;

import com.gph.saviorframework.Constants;
import com.gph.saviorframework.mq.service.TopicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/" + Constants.FRAMEWORK_REQUEST_PATH + "/mq/topic")
public class TopicController {

    /** 日志输出 */
    final Logger logger = LoggerFactory.getLogger(TopicController.class);

    /** 领域对象名称 */
    private final String domain = "topic";

    /** 视图前缀 */
    private final String viewPrefix = Constants.FRAMEWORK_REQUEST_PATH + "/" + "mq" + "/" + domain + "/" + domain + "_";

    @Autowired
    private TopicService topicService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String topicListPage(){
        return viewPrefix + "list";
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap getTopicList(){
        ModelMap modelMap = new ModelMap();
        try{
            List<String> topics = topicService.getTopics();

            List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
            for(String topicName : topics){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("topicName",topicName);

                list.add(map);
            }
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.TRUE);
            modelMap.addAttribute(Constants.DEFAULT_RECORD_MODEL_KEY,list);
        }
        catch (Exception ex){
            ex.printStackTrace();
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.FALSE);
            modelMap.addAttribute(Constants.ERROR_INFO, ex.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "/create",method = RequestMethod.GET)
    public String topicCreatePage(){
        return viewPrefix + "create";
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap addTopic(
            @RequestParam(value = "topicName",required = true)String topicName,
            @RequestParam(value = "readQueueNums",required = false)String readQueueNums,
            @RequestParam(value = "writeQueueNums",required = false)String writeQueueNums,
            @RequestParam(value = "perm",required = false)String perm,
            @RequestParam(value = "brokerAddr",required = false)String brokerAddr,
            @RequestParam(value = "clusterName",required = false)String clusterName){

        ModelMap modelMap = new ModelMap();

        try {
            boolean isOk = topicService.update(topicName,readQueueNums,writeQueueNums,perm,brokerAddr,clusterName);
            if(isOk){
                modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.TRUE);
            }
            else {
                modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.FALSE);
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
            modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.FALSE);
            modelMap.addAttribute(Constants.ERROR_INFO, ex.getMessage());
        }

        return modelMap;
    }
}
